package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysEmp;
import com.ruoyi.system.service.ISysEmpService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位信息操作处理
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/emp")
public class SysEmpController extends BaseController
{
    private String prefix = "system/emp";

    @Autowired
    private ISysEmpService empService;

    @RequiresPermissions("system:emp:view")
    @GetMapping()
    public String operlog()
    {
        return prefix + "/emp";
    }

    @RequiresPermissions("system:emp:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysEmp emp)
    {
        startPage();
        List<SysEmp> list = empService.selectEmpList(emp);
        return getDataTable(list);
    }

    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:emp:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysEmp emp)
    {
        List<SysEmp> list = empService.selectEmpList(emp);
        ExcelUtil<SysEmp> util = new ExcelUtil<SysEmp>(SysEmp.class);
        return util.exportExcel(list, "岗位数据");
    }

    @RequiresPermissions("system:emp:remove")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(empService.deleteEmpByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 新增岗位
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存岗位
     */
    @RequiresPermissions("system:emp:add")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysEmp emp)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(empService.checkEmpNameUnique(emp)))
        {
            return error("新增岗位'" + emp.getEmpName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.POST_CODE_NOT_UNIQUE.equals(empService.checkEmpCodeUnique(emp)))
        {
            return error("新增岗位'" + emp.getEmpName() + "'失败，岗位编码已存在");
        }
        emp.setCreateBy(getLoginName());
        return toAjax(empService.insertEmp(emp));
    }

    /**
     * 修改岗位
     */
    @RequiresPermissions("system:emp:edit")
    @GetMapping("/edit/{empId}")
    public String edit(@PathVariable("empId") Long empId, ModelMap mmap)
    {
        mmap.put("emp", empService.selectEmpById(empId));
        return prefix + "/edit";
    }

    /**
     * 修改保存岗位
     */
    @RequiresPermissions("system:emp:edit")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysEmp emp)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(empService.checkEmpNameUnique(emp)))
        {
            return error("修改岗位'" + emp.getEmpName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.POST_CODE_NOT_UNIQUE.equals(empService.checkEmpCodeUnique(emp)))
        {
            return error("修改岗位'" + emp.getEmpName() + "'失败，岗位编码已存在");
        }
        emp.setUpdateBy(getLoginName());
        return toAjax(empService.updateEmp(emp));
    }

    /**
     * 校验岗位名称
     */
    @PostMapping("/checkEmpNameUnique")
    @ResponseBody
    public String checkEmpNameUnique(SysEmp emp)
    {
        return empService.checkEmpNameUnique(emp);
    }

    /**
     * 校验岗位编码
     */
    @PostMapping("/checkEmpCodeUnique")
    @ResponseBody
    public String checkEmpCodeUnique(SysEmp emp)
    {
        return empService.checkEmpCodeUnique(emp);
    }
}
